const crypto = require('crypto')

/*
  科大讯飞鉴权参数：
  host - 请求主机 tts-api.xfyun.cn
  date - 当前时间戳，RFC1123格式
  authorization - 使用base64编码的签名相关信息（签名基于hmac-sha256计算）
      authorization_origin: api_key="$api_key",algorithm="hmac-sha256",headers="host date request-line",signature="$signature"
      signature - 原始字段由host，date，request-line三个参数按照格式拼接成：
      host: $host\ndate: $date\n$request-line
 */

function createVoice (req, res) {
  const text = req.query.text
  const lang = req.query.lang
  // const text = '测试科大讯飞在线语音合成api的功能，比如说，我们输入一段话，科大讯飞api会在线实时生成语音返回给客户端。'
  // const lang = 'cn'
  let engineType = 'initp65'
  if (lang.toLowerCase() === 'en') {
    engineType = 'intp65_en'
  }
  const speed = '30'
  const voiceParam = {
    auf: 'audio/L16;rate=16000',
    aue: 'lame',
    voice_name: 'xiaoyan',
    speed,
    volume: '50',
    pitch: '50',
    engine_type: engineType,
    text_type: 'text'
  }
  const host = '210.83.229.241:8000'
  const date = new Date().toGMTString()
  const apiKey = '2fbd10928cb9121b65d637a896213d78'
  const apiSecret = 'ba54fa7ba6ee3fab66746513a2533cf1'
  const requestLine = 'GET /v2/tts HTTP/1.1'
  const signature_origin = `host: ${host}\ndate: ${date}\n${requestLine}`
  const hs = crypto.createHmac('sha256', apiSecret)
  hs.update(signature_origin)
  const signature = hs.digest('base64')
  console.log(`signature: ${signature}`)
  const authorization_origin = `api_key="${apiKey}",algorithm="hmac-sha256",headers="host date request-line",signature="${signature}"`
  const authorization = btoa(authorization_origin)
  url = `wss://tts-api.xfyun.cn/v2/tts?authorization=${authorization}&date=${date}&host=${host}`
  
}

module.exports = createVoice